package edu.fafu.dao.impl;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.stereotype.Repository;

import edu.fafu.dao.ILoginDao;
import edu.fafu.entity.Admin;
import edu.fafu.entity.User;
import edu.fafu.util.HibernateSupport;

/**
 * @author asus
 *
 */
@Repository("loginDao")
public class LoginDao extends HibernateSupport implements ILoginDao{
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	@Override
	public Admin findById(int id) {
		Session openSession = sessionFactory.openSession();
		Query q = openSession.createQuery("from Admin where id="+id);
		return (Admin)q.list().get(0);
	}

	@Override
	public User findUser(User u) {
		String Hql = "from User u where u.username='"+u.getUsername()+"' and u.pwd= '"+u.getPwd()+"'";
		User user = (User)getListOne(Hql);
		return user;
	}
	
	@Override
	public void addUser(User u) {
		add(u);		
	}

	@Override
	public void updateUser(User user) {
		update(user);
	}
	
}
